package cn.edu.scau.cmi.chenzehan.client;

import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPICCFactory.PICCAutomobileDamageFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPICCFactory.PICCCasualtiesFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPICCFactory.PICCDriverInjuredFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPICCFactory.PICCMultipAccidentFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPingAnFactory.PingAnAutomobileDamageFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPingAnFactory.PingAnCasualtiesFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPingAnFactory.PingAnDriverInjuredFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.domainPingAnFactory.PingAnMultipleAccidentFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.factory.AbstractFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.factory.PICCFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.factory.PingAnFactory;
import cn.edu.scau.cmi.chenzehan.abstractFactory.insurance.Insurance;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class UiAbstractFactory extends Application {
	
	

	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("abstractFactory.fxml"));
		Button button = (Button) root.lookup("#button1");
		TextArea textArea = (TextArea) root.lookup("#textfield1");
		ChoiceBox<String> choiceBox1 = (ChoiceBox) root.lookup("#choiceBox1");
		ChoiceBox<String> choiceBox2 = (ChoiceBox) root.lookup("#choiceBox2");
		choiceBox1.setItems(FXCollections.observableArrayList("中国人寿保险(PICC)", "平安保险(PingAn)"));
		choiceBox2.setItems(FXCollections.observableArrayList("驾驶员身体受伤", "汽车损坏", "人员伤亡","多种事故"));

		textArea.setEditable(false);
		choiceBox1.setOnAction(e->{
			textArea.appendText(AbstractFactory.getFactory(choiceBox1.getValue()).getMessage()+"\n");
		});
		button.setOnAction(e -> {
			if (choiceBox1.getValue() == null) {
				textArea.appendText("请指定保险公司!" + "\n");
			} else if (choiceBox2.getValue() == null) {
				textArea.appendText("请选择一种保险!" + "\n");
			} else {
				AbstractFactory af = AbstractFactory.getFactory(choiceBox1.getValue());
				 if(af instanceof PICCFactory) {
					 af = (PICCFactory) af;
				 }
				 else {
					 af = (PingAnFactory) af;
				 }
				 af = af.getDetailFactory(choiceBox2.getValue());
				if(af instanceof PICCAutomobileDamageFactory) af = (PICCAutomobileDamageFactory) af;
				if(af instanceof PICCCasualtiesFactory) af = (PICCCasualtiesFactory) af;
				if(af instanceof PICCDriverInjuredFactory) af = (PICCDriverInjuredFactory) af;
				if(af instanceof PICCMultipAccidentFactory) af = (PICCMultipAccidentFactory) af;
				if(af instanceof PingAnAutomobileDamageFactory) af = (PingAnAutomobileDamageFactory) af;
				if(af instanceof PingAnCasualtiesFactory) af = (PingAnCasualtiesFactory) af;
				if(af instanceof PingAnDriverInjuredFactory) af = (PingAnDriverInjuredFactory) af;
				if(af instanceof PingAnMultipleAccidentFactory) af = (PingAnMultipleAccidentFactory) af;
				Insurance is = af.creatProduct();
				textArea.appendText(is.getMessage()+"\n");
				
			}});
		Scene scene = new Scene(root);

		primaryStage.setTitle("实验二:抽象工厂方法");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
